﻿using ECommerce.Web.NHibernate.Sessions;
using NHibernate;

namespace ECommerce.Web.Repositories
{
    public class Repository<T> : IRepository<T> where T : class, new()
    {
        protected ISession Session
        {
            get { return SessionSource.Current.GetSession(); }
        }
        
        public virtual T FindById(int id)
        {
            return Session.FindById<T>(id);          
        }

        public virtual void Save(T entity)
        {
            using (ITransaction transaction = Session.BeginTransaction())
            {
                Session.SaveOrUpdate(entity);

                transaction.Commit();
            }
        }

        public virtual void Delete(T entity)
        {
            using (ITransaction transaction = Session.BeginTransaction())
            {
                Session.Delete(entity);

                transaction.Commit();
            }
        }
    }
}
